﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Code.Models
{
    public partial class Issue
    {
        /// <summary>
        /// 获取格式化后的期号 或 设置要格式化的期号 (LotteryID 不能为空)
        /// </summary>
        public string FormatIssueName
        {
            get { return PastIssueName((LotteryType)LotteryID, Number); }
            set { Number = BulidIssueName((LotteryType)LotteryID,value); }
            
        }

        /// <summary>
        /// 格式化数据库取出的期号
        /// </summary>
        /// <param name="type">彩种</param>
        /// <param name="issueName">数据库的期号</param>
        /// <returns>格式化后的期号</returns>
        public static string PastIssueName(LotteryType type, string issueName)
        {
            try
            {
                switch (type)
                {
                    case LotteryType.HLJSSC:
                        string hljissue = System.Configuration.ConfigurationManager.AppSettings["HLJSSC_ReferenceIssue"];
                        string wintime = System.Configuration.ConfigurationManager.AppSettings["HLJSSC_ReferenceTime"];
                        string strDate = issueName.Substring(0, 8);
                        string strOldQh = issueName.Substring(9, 2);
                        string strGetDate = strDate.Substring(0, 4) + "-" + strDate.Substring(4, 2) + "-" + strDate.Substring(6, 2) + " 10:10:01";
                        TimeSpan dtDays = Convert.ToDateTime(strGetDate) - Convert.ToDateTime(wintime);
                        int numNewQh = dtDays.Days * 84 + Convert.ToInt32(strOldQh) + Convert.ToInt32(hljissue) - 1;
                        int numLength = numNewQh.ToString().Trim().Length;
                        string strNewQh = numNewQh.ToString();
                        for (int i = 0; i < 7 - numLength; i++)
                        {
                            strNewQh = "0" + strNewQh;
                        }
                        issueName = strNewQh;
                        break;

                    case LotteryType.SZPL:
                        string szplissue = System.Configuration.ConfigurationManager.AppSettings["SZPL_ReferenceIssue"];
                        string szplwintime = System.Configuration.ConfigurationManager.AppSettings["SZPL_ReferenceTime"];
                        if (string.IsNullOrEmpty(szplissue) || string.IsNullOrEmpty(szplwintime))
                            throw new System.IO.IOException("读取" + LotteryType.SZPL + "期号参照时出错，缺少SZPL_ReferenceIssue或SZPL_ReferenceTime配置节");
                        szplissue = szplissue.Trim();
                        szplwintime.Trim();

                        DateTime tmpdate;
                        int tmpissue;

                        if (!int.TryParse(szplissue.Length == 5 ? DateTime.Now.Year / 100 + szplissue : szplissue, out tmpissue) || !DateTime.TryParse(szplwintime, out tmpdate))
                            throw new System.ArithmeticException("SZPL_ReferenceIssue或SZPL_ReferenceTime配置节解析出错");

                        int span = (tmpdate.Year * 1000 + tmpdate.DayOfYear) - tmpissue;

                        int issuespan = int.Parse(issueName) - span;
                        issueName = issuespan.ToString();
                        break;
                    case LotteryType.FC3D:
                        string fc3dissue = System.Configuration.ConfigurationManager.AppSettings["FC3D_ReferenceIssue"];
                        string fc3dwintime = System.Configuration.ConfigurationManager.AppSettings["FC3D_ReferenceTime"];
                        if (string.IsNullOrEmpty(fc3dissue) || string.IsNullOrEmpty(fc3dwintime))
                            throw new System.IO.IOException("读取" + LotteryType.SZPL + "期号参照时出错，缺少FC3D_ReferenceIssue或FC3D_ReferenceTime配置节");
                        fc3dissue = fc3dissue.Trim();
                        fc3dwintime.Trim();

                        DateTime tmpdate1;
                        int tmpissue1;

                        if (!int.TryParse(fc3dissue.Length == 5 ? DateTime.Now.Year / 100 + fc3dissue : fc3dissue, out tmpissue1) || !DateTime.TryParse(fc3dwintime, out tmpdate1))
                            throw new System.ArithmeticException("FC3D_ReferenceIssue或FC3D_ReferenceTime配置节解析出错");

                        int span1 = (tmpdate1.Year * 1000 + tmpdate1.DayOfYear) - tmpissue1;

                        int issuespan1 = int.Parse(issueName) - span1;
                        issueName = issuespan1.ToString();
                        break;
                    case LotteryType.HaoCai1:
                        string hc1issue = System.Configuration.ConfigurationManager.AppSettings["HaoCai1_ReferenceIssue"];
                        string hc1wintime = System.Configuration.ConfigurationManager.AppSettings["HaoCai1_ReferenceTime"];
                        if (string.IsNullOrEmpty(hc1issue) || string.IsNullOrEmpty(hc1wintime))
                            throw new System.IO.IOException("读取" + LotteryType.SZPL + "期号参照时出错，缺少HaoCai1_ReferenceIssue或HaoCai1_ReferenceTime配置节");
                        hc1issue = hc1issue.Trim();
                        hc1wintime.Trim();

                        DateTime tmpdate2;
                        int tmpissue2;

                        if (!int.TryParse(hc1issue.Length == 5 ? DateTime.Now.Year / 100 + hc1issue : hc1issue, out tmpissue2) || !DateTime.TryParse(hc1wintime, out tmpdate2))
                            throw new System.ArithmeticException("HaoCai1_ReferenceIssue或HaoCai1_ReferenceTime配置节解析出错");

                        int span2 = (tmpdate2.Year * 1000 + tmpdate2.DayOfYear) - tmpissue2;

                        int issuespan2 = int.Parse(issueName) - span2;
                        issueName = issuespan2.ToString();
                        break;
                }
            }
            catch (Exception e) { var Log = Code.Commen.Log.GetLog("Code.Models.Issue"); 
                if(Log.IsDebugEnabled)
                    Log.Debug("期号格式化出错：",e);
                else if(Log.IsErrorEnabled)
                    Log.Error("期号格式化出错：" + e.Message);
                else if (Log.IsInfoEnabled)
                    Log.Info("期号格式化出错："+ e.Message);
            }
            return issueName;
        }

        /// <summary>
        /// 解析期号，用于存到数据库
        /// </summary>
        /// <param name="type">彩种</param>
        /// <param name="issueName">要解析的期号</param>
        /// <returns>和数据库的期号一样的格式</returns>
        public static string BulidIssueName(LotteryType type, string issueName)
        {
            switch (type)
            {
                case (LotteryType.HLJSSC):
                    string hljissue = System.Configuration.ConfigurationManager.AppSettings["HLJSSC_ReferenceIssue"];
                    string wintime = System.Configuration.ConfigurationManager.AppSettings["HLJSSC_ReferenceTime"];
                    int numNewQH = Convert.ToInt32(issueName);
                    int numOldQH = Convert.ToInt32(hljissue);
                    int numSpanQH = numNewQH - numOldQH;
                    int numDaySpan = numSpanQH / 84;         //间隔天数
                    int numDayQhs = numSpanQH - numDaySpan * 84;  //当天间隔期数
                    string strDayQhs = "";
                    if (numDayQhs < 9)
                    {
                        strDayQhs = "0" + (numDayQhs + 1).ToString();
                    }
                    else
                    {
                        strDayQhs = (numDayQhs + 1).ToString();
                    }

                    DateTime dtNewTime = Convert.ToDateTime(wintime).AddDays(numDaySpan);
                    issueName = dtNewTime.ToString("yyyyMMdd") + "-" + strDayQhs;
                    break;
                case LotteryType.SZPL:
                    string szplissue = System.Configuration.ConfigurationManager.AppSettings["SZPL_ReferenceIssue"];
                    string szplwintime = System.Configuration.ConfigurationManager.AppSettings["SZPL_ReferenceTime"];
                    if (string.IsNullOrEmpty(szplissue) || string.IsNullOrEmpty(szplwintime))
                        throw new System.IO.IOException("读取" + LotteryType.SZPL + "期号参照时出错，缺少SZPL_ReferenceIssue或SZPL_ReferenceTime配置节");
                    szplissue = szplissue.Trim();
                    szplwintime.Trim();

                    DateTime tmpdate;
                    int tmpissue;

                    if (!int.TryParse(szplissue.Length == 5 ? DateTime.Now.Year / 100 + szplissue : szplissue, out tmpissue) || !DateTime.TryParse(szplwintime, out tmpdate))
                        throw new System.ArithmeticException("SZPL_ReferenceIssue或SZPL_ReferenceTime配置节解析出错");

                    int span = (tmpdate.Year * 1000 + tmpdate.DayOfYear) - tmpissue;

                    int issuespan = int.Parse(issueName) + span;
                    issueName = issuespan.ToString();
                    break;
                case LotteryType.FC3D:
                    string fc3dissue = System.Configuration.ConfigurationManager.AppSettings["FC3D_ReferenceIssue"];
                    string fc3dwintime = System.Configuration.ConfigurationManager.AppSettings["FC3D_ReferenceTime"];
                    if (string.IsNullOrEmpty(fc3dissue) || string.IsNullOrEmpty(fc3dwintime))
                        throw new System.IO.IOException("读取" + LotteryType.SZPL + "期号参照时出错，缺少FC3D_ReferenceIssue或FC3D_ReferenceTime配置节");
                    fc3dissue = fc3dissue.Trim();
                    fc3dwintime.Trim();

                    DateTime tmpdate1;
                    int tmpissue1;

                    if (!int.TryParse(fc3dissue.Length == 5 ? DateTime.Now.Year / 100 + fc3dissue : fc3dissue, out tmpissue1) || !DateTime.TryParse(fc3dwintime, out tmpdate1))
                        throw new System.ArithmeticException("FC3D_ReferenceIssue或FC3D_ReferenceTime配置节解析出错");

                    int span1 = (tmpdate1.Year * 1000 + tmpdate1.DayOfYear) - tmpissue1;

                    int issuespan1 = int.Parse(issueName) + span1;
                    issueName = issuespan1.ToString();
                    break;
                case LotteryType.HaoCai1:
                    string hc1dissue = System.Configuration.ConfigurationManager.AppSettings["HaoCai1_ReferenceIssue"];
                    string hc1dwintime = System.Configuration.ConfigurationManager.AppSettings["HaoCai1_ReferenceTime"];
                    if (string.IsNullOrEmpty(hc1dissue) || string.IsNullOrEmpty(hc1dwintime))
                        throw new System.IO.IOException("读取" + LotteryType.SZPL + "期号参照时出错，缺少HaoCai1_ReferenceIssue或HaoCai1_ReferenceTime配置节");
                    hc1dissue = hc1dissue.Trim();
                    hc1dwintime.Trim();

                    DateTime tmpdate2;
                    int tmpissue2;

                    if (!int.TryParse(hc1dissue.Length == 5 ? DateTime.Now.Year / 100 + hc1dissue : hc1dissue, out tmpissue2) || !DateTime.TryParse(hc1dwintime, out tmpdate2))
                        throw new System.ArithmeticException("HaoCai1_ReferenceIssue或HaoCai1_ReferenceTime配置节解析出错");

                    int span2 = (tmpdate2.Year * 1000 + tmpdate2.DayOfYear) - tmpissue2;

                    int issuespan2 = int.Parse(issueName) + span2;
                    issueName = issuespan2.ToString();
                    break;
            }

            return issueName;
        }

        /// <summary>
        /// 当前期号彩种的名称
        /// </summary>
        public string LotteryName { get; set; }
    }
}